<?php

namespace App\Http\Controllers\Api\V1\Requests;

use Rainsens\Dinglog\Facades\Dinglog;
use Closure;
use App\Supports\Sms\Sms;
use App\Http\Controllers\Api\V1\Requests\FormRequest;


/**
 * Class AuthStoreRequest
 * @package App\Http\Controllers\Api\V1\Requests
 *
 * @property $phone
 * @property $password
 * @property $captcha_key
 * @property $captcha_code
 */
class AuthStoreRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'phone' => 'required|phone:CN,mobile',
            'sms_key' => 'required|string',
            'sms_code' => ['required', 'string', 'min:6', 'max:6', $this->smsCode()],
            'agreement' => ['required', 'boolean', $this->agreement()],
        ];
    }

	public function attributes(): array
	{
		return [
			'phone' => '手机号码',
            'sms_key' => '短信验证码密钥',
            'sms_code' => '短信验证码',
            'agreement' => '用户协议'
		];
	}

    public function smsCode(): Closure
    {
        return function ($attribute, $value, $fail) {
            $sms = app(Sms::class);
            $verification = $sms->isSmsDataRight(request('sms_key'), request('phone'), $value);
            if (! $verification) {
                return $fail('短信验证码无效');
            }
            return true;
        };
    }


    public function agreement(): Closure
    {
        return function ($attribute, $value, $fail) {
            if (! $value) {
                return $fail('请先阅读用户协议');
            }
            return true;
        };
    }
}
